<template>
  <view class="related-content">
    <view class="top-block view-block">
      <uni-section title="实时信息" type="line" titleColor=" #102C59" titleFontSize="32rpx"></uni-section>

      <view class="list-card">
        <view class="list-title">变频器一主风机</view>

        <uni-row class="row-list" :gutter="170">
          <uni-col :span="8" class="col-item" v-for="(item, index) in datas1.data1" :key="index + '_dats1'">
            <view class="lab">{{ item.name }}</view>
            <view class="val2">{{ item.value }}</view>
          </uni-col>
        </uni-row>
      </view>

      <view class="list-card">
        <view class="list-title">变频器二备用风机</view>
        <uni-row class="row-list" :gutter="170">
          <uni-col :span="8" class="col-item" v-for="(item, index) in datas2.data1" :key="index + '_dats3'">
            <view class="lab">{{ item.name }}</view>
            <view class="val2">{{ item.value }}</view>
          </uni-col>
          
        </uni-row>
      </view>

      <view class="list-card">
        <view class="list-title">变频器三-主风机</view>
        <uni-row class="row-list" :gutter="170">
          <uni-col :span="8" class="col-item" v-for="(item, index) in datas4.data1" :key="index + '_dats5'" >
            <view class="lab">{{ item.name }}</view>
            <view class="val2">{{ item.value }}</view>
          </uni-col>
          
        </uni-row>
      </view>

      <view class="list-card">
        <view class="list-title">变频器四备用风机</view>
        <uni-row class="row-list" :gutter="170">
          <uni-col :span="8" class="col-item"  v-for="(item, index) in datas3.data1" :key="index + '_dats7'">
            <view class="lab">{{ item.name }}</view>
            <view class="val2">{{ item.value }}</view>
          </uni-col>
          
        </uni-row>
      </view>
    </view>
  </view>
</template>

<script>
import { getDeviceRunInfo } from '@/api/monitorCenter/index.js';

export default {
  data() {
    return {
      datas1: {},
      datas2: {},
      datas3: {},
      datas4: {}
    };
  },
  created() {
    this.getData();
  },
  methods: {
    async getData() {
      const [res1, res2, res3, res4] = await Promise.all([
        getDeviceRunInfo('tfj_one'), 
        getDeviceRunInfo('tfj_two'), 
        getDeviceRunInfo('tfj_three'), 
        getDeviceRunInfo('tfj_four')
      ]);
      
      if (res1.code != 200 || res2.code != 200 || res3.code != 200 || res4.code != 200) return;
      this.datas1 = this.formatDeviceData(res1.data, 1);
      this.datas2 = this.formatDeviceData(res2.data, 2);
      this.datas3 = this.formatDeviceData(res3.data, 3);
      this.datas4 = this.formatDeviceData(res4.data, 4);
    },
    formatDeviceData(data, type) {
      const dataMapping = {
        1: { data1: this.mapMonitorData1(data), data2: this.mapMonitorData2(data) },
        2: { data1: this.mapMonitorData3(data), data2: this.mapMonitorData4(data) },
        3: { data1: this.mapMonitorData5(data), data2: this.mapMonitorData6(data) },
        4: { data1: this.mapMonitorData7(data), data2: this.mapMonitorData8(data) }
      };
      return dataMapping[type] || { data1: [], data2: [] };
    },

    mapMonitorData1(data) {
      return [
        { name: '运行状态', value: this.statusMap(data.t_330039) },
        { name: '输出频率', value: this.handlePL(data.t_330007) + 'Hz' },
        { name: '频率指令', value: this.handlePLZL(data.t_330006) + 'Hz' },
        { name: '输出电流', value: this.handleDL(data.t_330009) + 'A' },
        { name: '输出电压', value: (data.t_330008 || 0) + 'V' },
        { name: '输出功率', value: (data.t_330010 || 0) + 'KW' }
      ];
    },
    mapMonitorData2(data) {
      return [
        { name: '主回路过压', value: data.t_330059 || 0 },
        { name: '接地短路', value: data.t_330069 || 0 },
        { name: '变频器过载', value: data.t_330060 || 0 },
        { name: '变频器过热', value: data.t_330090 || 0 },
        { name: '控制电源故障', value: data.t_330066 || 0 },
        { name: '冷却风扇故障', value: data.t_330084 || 0 },
        { name: '电机过热', value: data.t_330085 || 0 },
        { name: '散热片过热', value: data.t_330061 || 0 },
        { name: '电机过载', value: data.t_330074 || 0 }
      ];
    },

    mapMonitorData3(data) {
      return [
        { name: '运行状态', value: this.statusMap(data.t_190066) },
        { name: '输出频率', value: this.handlePL(data.t_190034) + 'Hz' },
        { name: '频率指令', value: this.handlePLZL(data.t_190033) + 'Hz' },
        { name: '输出电流', value: this.handleDL(data.t_190036) + 'A' },
        { name: '输出电压', value: (data.t_190035 || 0) + 'V' },
        { name: '输出功率', value: (data.t_190037 || 0) + 'KW' }
      ];
    },
    mapMonitorData4(data) {
      return [
        { name: '主回路过压', value: data.t_190086 || 0 },
        { name: '接地短路', value: data.t_190096 || 0 },
        { name: '变频器过载', value: data.t_190087 || 0 },
        { name: '变频器过热', value: data.t_190117 || 0 },
        { name: '控制电源故障', value: data.t_190093 || 0 },
        { name: '冷却风扇故障', value: data.t_190111 || 0 },
        { name: '电机过热', value: data.t_190112 || 0 },
        { name: '散热片过热', value: data.t_190088 || 0 },
        { name: '电机过载', value: data.t_190101 || 0 }
      ];
    },

    mapMonitorData5(data) {
      return [
        { name: '运行状态', value: this.statusMap(data.t_340041) },
        { name: '输出频率', value: this.handlePL(data.t_340007) + 'Hz' },
        { name: '频率指令', value: this.handlePLZL(data.t_340006) + 'Hz' },
        { name: '输出电流', value: this.handleDL(data.t_340009) + 'A' },
        { name: '输出电压', value: (data.t_340008 || 0) + 'V' },
        { name: '输出功率', value: (data.t_340010 || 0) + 'KW' }
      ];
    },
    mapMonitorData6(data) {
      return [
        { name: '主回路过压', value: data.t_340061 || 0 },
        { name: '接地短路', value: data.t_340071 || 0 },
        { name: '变频器过载', value: data.t_340062 || 0 },
        { name: '变频器过热', value: data.t_340092 || 0 },
        { name: '控制电源故障', value: data.t_340068 || 0 },
        { name: '冷却风扇故障', value: data.t_340086 || 0 },
        { name: '电机过热', value: data.t_340087 || 0 },
        { name: '散热片过热', value: data.t_340063 || 0 },
        { name: '电机过载', value: data.t_340076 || 0 }
      ];
    },

    mapMonitorData7(data) {
      return [
        { name: '运行状态', value: this.statusMap(data.t_350038) },
        { name: '输出频率', value: this.handlePL(data.t_350007) + 'Hz' },
        { name: '频率指令', value: this.handlePLZL(data.t_350006) + 'Hz' },
        { name: '输出电流', value: this.handleDL(data.t_350009) + 'A' },
        { name: '输出电压', value: (data.t_350008 || 0) + 'V' },
        { name: '输出功率', value: (data.t_350010 || 0) + 'KW' }
      ];
    },
    mapMonitorData8(data) {
      return [
        { name: '主回路过压', value: data.t_350058 || 0 },
        { name: '接地短路', value: data.t_350068 || 0 },
        { name: '变频器过载', value: data.t_350059 || 0 },
        { name: '变频器过热', value: data.t_350089 || 0 },
        { name: '控制电源故障', value: data.t_350065 || 0 },
        { name: '冷却风扇故障', value: data.t_350083 || 0 },
        { name: '电机过热', value: data.t_350084 || 0 },
        { name: '散热片过热', value: data.t_350060 || 0 },
        { name: '电机过载', value: data.t_350073 || 0 }
      ];
    },
    statusMap(value) {
      if (value != '--' && parseInt(value) == 1) {
        return '运行';
      }
      return '备用';
    },
    //频率
    handlePL(value) {
      if (value && value != '--') {
        return parseFloat((value * 0.01).toFixed(10));
      }
      return '--';
    },
    //频率指令
    handlePLZL(value) {
      if (value && value != '--') {
        return parseFloat((value * 0.01).toFixed(10));
      }
      return '--';
    },
    //输出电流
    handleDL(value) {
      if (value && value != '--') {
        return parseFloat((value / 20).toFixed(10));
      }
      return '--';
    }
  }
};
</script>

<style lang="scss" scoped>
.related-content {
  overflow-y: auto;
  
  .top-block {
    // box-shadow: 0rpx -15rpx 21rpx -5rpx rgba(0, 0, 0, 0.25);
    border-radius: 20rpx 20rpx 0rpx 0rpx;
  }

  .view-block {
    background: #fff;
    padding: 20rpx 30rpx;
    margin-bottom: 40rpx;
  }

  .list-card {
    width: 100%;
    height: 262rpx;
    background: url('/static/images/monitor/tf-list-card.png') no-repeat;
    background-size: 100%;
    margin-bottom: 40rpx;
    padding: 18rpx 24rpx;

    .list-title {
      font-weight: 500;
      font-size: 32rpx;
      color: #102c59;
      padding-bottom: 2rpx;
    }

    .row-list {
      .col-item {
        font-size: 24rpx;

        padding-top: 12rpx;
        text-align: center;
        .lab {
          color: rgba(16, 44, 89, 0.5);
        }
        .val {
          color: rgba(16, 44, 89, 1);
        }
        .val2 {
          font-weight: bold;
          color: #2e7dff;
        }
      }
    }
  }
}

::v-deep .uni-section .uni-section-header {
  padding: 0 0 20rpx 0;
}
</style>
